package com.drought.rained.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.drought.rained.R;

public class UpdateVersionDialogLJ extends Dialog implements View.OnClickListener {

    private View rootView;
    private TextView tvTile; // 标题
    private TextView tvContent; // 内容
    private TextView btnConfirm; // 按钮
    private NormalClickListener mNormalClickListener;

    public UpdateVersionDialogLJ(@NonNull Context context) {
        super(context);
        init(context);
    }

    public UpdateVersionDialogLJ(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected UpdateVersionDialogLJ(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_update_version_dialog, null);
        tvTile = rootView.findViewById(R.id.tv_tile);
        tvContent = rootView.findViewById(R.id.tv_content);
        btnConfirm = rootView.findViewById(R.id.confirm_button);
        setContentView(rootView);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = context.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        btnConfirm.setOnClickListener(this);
    }

    public UpdateVersionDialogLJ setTitle(String title) {
        if (tvTile != null) {
            tvTile.setText(title);
        }
        return this;
    }

    public UpdateVersionDialogLJ setContent(String contentStr) {
        if (tvContent != null) {
            tvContent.setText(contentStr);
        }
        return this;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onConfirmClick();
                }
                break;
        }
    }

    public TextView getBtn() {
        return btnConfirm;
    }

    public UpdateVersionDialogLJ setNormalClickListener(NormalClickListener listener) {
        mNormalClickListener = listener;
        return this;
    }

    public UpdateVersionDialogLJ setBtnText(String btnText) {
        this.btnConfirm.setText(btnText);
        return this;
    }

    public interface NormalClickListener {
        void onConfirmClick();
    }
}
